<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author:kane < chengjin005@163.com>
// +----------------------------------------------------------------------
namespace app\hotel\controller;

use app\hotel\model\ReserveModel;
use app\hotel\model\HotelCateModel;
use cmf\controller\AdminBaseController;
use think\Db;

/**
 * Class ReserveController 订单控制器
 * @package app\hotel\controller
 */
class ReserveController extends AdminBaseController
{
    /**
     * 订单管理
     * @adminMenu(
     *     'name'   => '订单',
     *
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '订单',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $where   = [];
        $request = input('request.');

        if (!empty($request['openid'])) {
            $where['openid'] = intval($request['openid']);
        }
        if (!empty($request['room_id'])) {
            $where['room_id'] = intval($request['room_id']);
        }
        if (!empty($request['name'])) {
            $keyword = $request['name'];
            $where['name']    = ['like', "%$keyword%"];
        }
        if (!empty($request['start_time'])) {

            $where['checkin_time']    = ['>', $request['start_time']];
        }
        if (!empty($request['end_time'])) {

            $where['departure_time']    = ['>', $request['end_time']];
        }
        $hotelCateModel = new HotelCateModel();
        $categoryTree        = $hotelCateModel->field('id,name')->where(array('delete_time'=>0))->select();

        $this->assign('category_tree', $categoryTree);

        $ReserveModel = new ReserveModel();
        $order          = $ReserveModel->where($where)->paginate();
        foreach ($order as $k=>&$v){
            $room =  Db::name('hotel_cate')->where(array('id'=>$v['room_id']))->field('name')->find();
            $user =  Db::name('third_party_user')->where(array('id'=>$v['user_id']))->field('nickname,openid,avatar')->find();
            $v['room_name'] = ($room['name'])?:'';
            $v['nickname'] =  ($user['nickname'])?:'';
            $v['openid'] =  ($user['openid'])?:'';
            $v['avatar'] = ( $user['avatar'])?:'';


        }

        $this->assign("list", $order);

        $this->assign('page', $order->render());
        return $this->fetch();
    }


    /**
     * 添加订单提交
     * @adminMenu(
     *     'name'   => '添加订单提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加订单提交',
     *     'param'  => ''
     * )
     */
    public function addPost()
    {



    }

    /**
     * 更新订单状态
     * @adminMenu(
     *     'name'   => '更新标签状态',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '更新标签状态',
     *     'param'  => ''
     * )
     */
    public function upStatus()
    {
        $intId     = $this->request->param("id");
        $intStatus = $this->request->param("status");
        $intStatus = $intStatus ? 1 : 0;
        if (empty($intId)) {
            $this->error(lang("NO_ID"));
        }
        $ReserveModel = new ReserveModel();
        $ReserveModel->isUpdate(true)->save(["status" => $intStatus], ["id" => $intId]);
        $this->success('保存成功');

    }

    /**
     * 删除订单
     * @adminMenu(
     *     'name'   => '删除订单',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '删除订单',
     *     'param'  => ''
     * )
     */
    public function delete()
    {
        $intId = $this->request->param("id", 0, 'intval');

        if (empty($intId)) {
            $this->error(lang("NO_ID"));
        }
        $ReserveModel = new ReserveModel();

        $ReserveModel->where(['id' => $intId])->delete();
        Db::name('portal_tag_post')->where('tag_id', $intId)->delete();
        $this->success('删除成功');
    }
}
